package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHttpServerProxyClientTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;


@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportServerProxyClientTransferTypeAdvanced
        extends AbstractHttpServerProxyClientTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext       当前通道
     * @param nettyProxyMsg 通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {

        Channel channel = nettyChannelContext.channel();
        // 创建链接、发送数据
        String targetPortString = nettyProxyMsg.getTargetPortString();
        String targetIpString = nettyProxyMsg.getTargetIpString();
        byte[] data = nettyProxyMsg.getData();
        byte[] visitorId = nettyProxyMsg.getVisitorId();
        byte[] clientId = nettyProxyMsg.getClientId();
        String msgVisitorId = new String(visitorId);
        ChannelAttributeKeyUtils.buildClientId(channel, clientId);
        ChannelAttributeKeyUtils.buildVisitorId(channel, msgVisitorId);

        NettyTransferChannelContext.pushVisitor(channel, msgVisitorId);
        NettyByteBuf nettyByteBuf = new NettyByteBuf();
        nettyByteBuf.setData(data);
        ChannelAttributeKeyUtils.buildNettyByteBufData(channel, nettyByteBuf);
        // 获取客户端心跳通道
        Channel loadBalance = ChannelContext.getLoadBalance(clientId);

        NettyProxyMsg clientConnectTagetNettyProxyMsg = new NettyProxyMsg();
        clientConnectTagetNettyProxyMsg.setVisitorId(msgVisitorId);
        clientConnectTagetNettyProxyMsg.setClientTargetIp(targetIpString);
        clientConnectTagetNettyProxyMsg.setClientTargetPort(Integer.parseInt(targetPortString));
        clientConnectTagetNettyProxyMsg.setClientId(clientId);
        clientConnectTagetNettyProxyMsg.setType(ProxyMessageType.HTTP_DISTRIBUTE_SERVER_PROXY_CLIENT_CONNECTION_SUCCESSFUL_);
        if (loadBalance != null) {
            // 让客户端主动创建一个数据传输通道
            loadBalance.writeAndFlush(clientConnectTagetNettyProxyMsg);
        }else {
            log.error("can not find target client:【】 nettyChannelContext",clientId);
        }

    }

}
